package com.example.administrator.listviewdemo;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.support.v4.media.session.PlaybackStateCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private List<Kyana> kyanas = new ArrayList<Kyana>();
    private List<Kyana> kyanas_new = new ArrayList<Kyana>();
    private KyanaAdapter adapter;
    private SwipeRefreshLayout swipeRefreshLayout;
    private void init(){
        kyanas.add(new Kyana(new String("123"),123));
    }
    private boolean inited = false;
  //  private  ProgressDialog progressDialog=new ProgressDialog(MainActivity.this);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //初始数据
        init();


       adapter = new KyanaAdapter(MainActivity.this,R.layout.kyana_layout,kyanas);
        ListView listView = (ListView)findViewById(R.id.list_view);
       listView.setAdapter(adapter);
        new downLoadData().execute("init");
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            }
        });
        //刷新逻辑
        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                String task = "fresh";
                new downLoadData().execute(task);
            }
        });

    }

    //后台数据下载
    public class downLoadData extends AsyncTask<String,Integer,Boolean> {
        private ProgressDialog progressDialog;
        private String url = "jdbc:mysql://583d434438e9b.gz.cdb.myqcloud.com:7993";
        private String ueser = "cdb_outerroot";
        private String pwd = "abc1230456";
        private String task;
        downLoadData(){
            if (inited==false) {
                progressDialog = new ProgressDialog(MainActivity.this);
                progressDialog.setTitle("请求数据库");
                progressDialog.setMessage("Loading...");
            }
        }
        @Override
        protected void onPreExecute() {
            if (!inited) progressDialog.show();
        }

        @Override
        protected Boolean doInBackground(String... voids) {
            task = voids[0];
            try {
             /*   Class.forName("com.mysql.jdbc.Driver");
                Connection connection = (Connection)DriverManager.getConnection(url,ueser,pwd);

                Statement statement = connection.createStatement();
                String query="use LogHorizon";
                statement.execute(query);  //使用数据库*/
               String    query = "SELECT * FROM member_tbl";
           //     ResultSet resultSet = statement.executeQuery(query);

                DatabaseQuery databaseQuery = new DatabaseQuery();
                //String log="use LogHorizon";
                databaseQuery.connectDatabase();
                ResultSet resultSet = databaseQuery.Query(query);

                while(resultSet.next()){
                    kyanas_new.add(new Kyana(new String(resultSet.getString(2)),resultSet.getInt(1)));
                }
                databaseQuery.closeCon();


               kyanas.clear();
                kyanas.addAll(kyanas_new);

            }catch(Exception e){
                e.printStackTrace();
                return false;
            }
            return true;
        }

        @Override
        protected void onPostExecute(Boolean aBoolean) {
            if (!inited)
            progressDialog.dismiss();
          if (aBoolean){

                adapter.notifyDataSetChanged();
              if (inited == false) {
                  inited = true;
                  Toast.makeText(MainActivity.this, "数据库连接成功", Toast.LENGTH_SHORT).show();
              }
              if(swipeRefreshLayout.isRefreshing()&&task =="fresh"){
                  swipeRefreshLayout.setRefreshing(false);
              }
            }
            else{
                if (swipeRefreshLayout.isRefreshing()){
                    swipeRefreshLayout.setRefreshing(false);
                }
                Toast.makeText(MainActivity.this,"数据库连接失败",Toast.LENGTH_SHORT).show();
            }
        }
    }
}
